// Copyright 2021 Guoyoko. All Rights Reserved.

#include "Asset/CustomizeFlowAsset.h"
#include "Node/CustomizeFlowTaskNode.h"


void UCustomizeFlowAsset::Begin()
{
	if (RootNode)
	{
		CurrentTaskNode = RootNode;
		CurrentTaskNode->BeginTask();
	}
}

void UCustomizeFlowAsset::Tick(float DeltaTime)
{
	if (CurrentTaskNode)
	{
		if (CurrentTaskNode->Result == ECusFTaskResult::Succeeded)
		{
			CurrentTaskNode->EndTask();
			CurrentTaskNode->Result = ECusFTaskResult::Idle;


			TArray<UCustomizeFlowFlowTaskNode*> CurrentChildTaskNodes;
			CurrentTaskNode->GetChildNodes(CurrentChildTaskNodes);
			if (CurrentChildTaskNodes.IsValidIndex(0))
			{
				CurrentTaskNode = CurrentChildTaskNodes[0];
				CurrentTaskNode->BeginTask();
			}
		}
		else if (CurrentTaskNode->Result == ECusFTaskResult::InProgress)
		{
			CurrentTaskNode->TickTask(DeltaTime);
		}
	}
}

void UCustomizeFlowAsset::End()
{
	if (CurrentTaskNode->Result == ECusFTaskResult::Succeeded)
	{
		CurrentTaskNode->EndTask();
	}
}

void UCustomizeFlowAsset::SetRootNode(UObject* InRootNode)
{
	UCustomizeFlowFlowTaskNode* TempNode = Cast<UCustomizeFlowFlowTaskNode>(InRootNode);
	if (TempNode)
	{
		RootNode = TempNode;
	}
}
